import actionType from '../actions/actionType'

const initState = 
    // 初始化数据 ,由于我使用了对象加数组，所以操作数据要加一个carts才能获取到列表数据
    [
        {
            id:1,
            title: 'Apple3',
            price: 999.33,
            amount: 30
        },
        {
            id:2,
            title: 'AppleX',
            price: 9999.33,
            amount: 10
        }
    ]


export default (state = initState, action) => {
    console.log("我是cart的reducers，执行的action为",action)
    console.log("我是cart的reducers，执行的state为",state)
    switch(action.type){
        case actionType.CART_ADD_AMOUNT:
            console.log("执行加法")
            return state.map(item => {
                if (item.id === action.payload.id) {
                    console.log("执行加法中+2")
                    item.amount += 1
                }
                return item;
            })
        case actionType.CART_SUB_AMOUNT:
                console.log("执行减法")
            return state.map(item => {
                if (item.id === action.payload.id) {
                    console.log("执行减法中-1")
                    item.amount -= 1
                }
                return item;
            })
        default:
            return state
    }
}